package com.xiaode.controller.admin.example;

import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.xiaode.validator.BaseValidator;

public class ExampleParamValidator extends BaseValidator {
    @Override
    protected void validate(Controller c) {
        //设置短路验证
        setShortCircuit(true);
        //取得方法url
        String url = getActionKey();
        //如果url为添加榜样
        if ("/aExample/findExample".equals(url)) {
            Kv kv = Kv.create();
            try {
                kv.setIfNotNull("name", c.getParaToBoolean("name")).setIfNotNull("nameValue", c.getPara("nameValue"))
                        .setIfNotNull("avatarUrl", c.getParaToBoolean("avatarUrl")).setIfNotNull("brief", c.getParaToBoolean("brief"))
                        .setIfNotNull("task", c.getParaToBoolean("task")).setIfNotNull("student", c.getParaToBoolean("student"))
                        .setIfNotNull("studentValue", c.getPara("studentValue")).setIfNotNull("grade", c.getParaToBoolean("gradeId"))
                        .setIfNotNull("gradeValue", c.getPara("gradeIdValue")).setIfNotNull("gradeSort", c.getPara("gradeIdSort"))
                        .setIfNotNull("teacher", c.getParaToBoolean("teacher")).setIfNotNull("account", c.getParaToBoolean("account"))
                        .setIfNotNull("reason", c.getParaToBoolean("reason")).setIfNotNull("addTime", c.getParaToBoolean("addTime"))
                        .setIfNotNull("useNum", c.getParaToBoolean("useNum")).setIfNotNull("clasId", c.getParaToBoolean("clasId"))
                        .setIfNotNull("clasIdValue", c.getPara("clasIdValue")).setIfNotNull("clasIdSort", c.getPara("clasIdSort"))
                        .setIfNotNull("state", c.getParaToBoolean("sumState")).setIfNotNull("stateValue", c.getPara("sumStateValue"))
                        .setIfNotNull("characterType", c.getParaToBoolean("characterType")).setIfNotNull("characterTypeValue", c.getPara("characterTypeValue"))
                        .setIfNotNull("briefValue", c.getPara("briefValue")).set("taskValue", c.getPara("taskValue"))
                        .setIfNotNull("founder", c.getParaToBoolean("founder")).setIfNotNull("schoolYear",c.getParaToBoolean("schoolYear"));
            }catch (Exception e){
                setRet(Ret.fail().set("error", kv));
                addError("error", kv.toJson());
            }

            c.setAttr("kv", kv);
        }
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(getRet());
    }
}
